package org.example.struct.array2;

import java.util.HashMap;
import java.util.Map;

/**
 * @author liyishan
 * @date 2023/10/31 09:40
 * @apiNote
 */

public class Test00974SubarraysDivByK {

    public static void main(String[] args) {
        int[] nums = {4,5,0,-2,-3,1};
        int count = test(nums,5);
        System.out.println(count);
    }

    public static int test(int[] nums, int k) {
        Map<Integer,Integer> map = new HashMap<>();
        map.put(0,1);
        int sum = 0,ans=0;
        for(int num:nums){
            sum+=num;
            int mod = (sum%k+k)%k;
            int same = map.getOrDefault(mod,0);
            ans+=same;
            map.put(mod,same+1);
        }
        return ans;
    }
}
